package com.example.luke.shengdangxiaoattendance.Model;

import android.content.Context;
import android.content.SharedPreferences;
import android.icu.util.Calendar;

import com.example.luke.shengdangxiaoattendance.Util.GlobalFunction;

import java.util.Date;

/**
 * Created by luke on 20/06/2017.
 */

public class Setting {


    public  static String defaultClassName="";


    public static  void initSetting(Context context){

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);
        defaultClassName=preferences.getString("defaultClassName",defaultClassName);


    }


    public static  void saveSetting(Context context, String className){

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);
        defaultClassName=className;

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("defaultClassName",defaultClassName);

        editor.apply();


    }

    public static boolean allowUpload(Context context){

        int dayOfWeek= java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
        int month= java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);

        String temp=month+"/"+dayOfWeek;

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);


       String value= preferences.getString("weekDay","0/0");

        //GlobalFunction.printTextByTag(temp+":"+value);
        if (temp.equals(value))
            return true;


        return false;

    }
    public static void saveDownloadDay(Context context){

        int day= java.util.Calendar.getInstance().get(java.util.Calendar.DAY_OF_MONTH);
        int month= java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("weekDay",month+"/"+day+"");

        editor.apply();

    }

    public static void saveRestartMark(Context context,String mark){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("restart",mark);

        editor.apply();

    }
    public static boolean allowRestart(Context context){

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);


        String value= preferences.getString("restart","1");

        if (value.equals("1"))
            return true;
        else
            return false;


    }


    public static boolean allowLoop(Context context){

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);


      return preferences.getBoolean("loop",false);



    }


    public static void saveLoopMode(Context context,boolean mark){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putBoolean("loop",mark);

        editor.apply();


    }

    public static boolean allowAppRestart(Context context){

        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);


        return preferences.getBoolean("AppRestartMark",true);



    }


    public static void saveAppRestartMark(Context context,boolean mark){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putBoolean("AppRestartMark",mark);

        editor.apply();

            GlobalFunction.printTextByTag("mark:"+mark);
    }


    public static void savePushFreq(Context context,String freq){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putInt("PushFreq",Integer.valueOf(freq));

        editor.apply();

    }

    public static int getPushFreq(Context context){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);


        return preferences.getInt("PushFreq",5);

    }



    public static void saveInstallDay(Context context){


        SharedPreferences preferences=context.getSharedPreferences("InstallDay", Context.MODE_PRIVATE);

        SharedPreferences.Editor editor=preferences.edit();
        editor.putString("InstallDay",GlobalFunction.convertDate(new Date(),"yyyy-MM-dd"));

        editor.apply();

    }


    public static Date getInstallDay(Context context){


        SharedPreferences preferences=context.getSharedPreferences("setting", Context.MODE_PRIVATE);

        String date=   preferences.getString("InstallDay",null);


        Date temp=null;

        if (date!=null){

            temp=GlobalFunction.convertStrToDate(date,"yyyy-MM-dd");

        }

        return temp;


    }


}
